package edu.avans.hartigehap.service.jpa;

import java.util.ArrayList;
import java.util.Properties;
 
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import edu.avans.hartigehap.service.WebMailService;
 
public class MailServiceImpl implements WebMailService {
 
	public void sendMail(String email) {

	    String[] destAddr = {"erik_vroon@hotmail.com","aartsbram@hotmail.com","armandoregilio@hotmail.com","maurits.buijs@gmail.com","annicklouer@gmail.com"};
	    String fromAddr = "klantenservice@hartigehap.com";
		
		final String username = "hartigehap@gmail.com";
		final String password = "DVH11IA4";
 
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
 
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
 
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(fromAddr));
//			copy to hartyorders
			message.setRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse("hartigeorders@gmail.com"));
			message.setSubject("Nieuwe bestelling Hartige Hap");
			message.setText("Geachte Erco,\n\n"
				+ "Uw bestelling is geplaatst. \n\n" 
				+ "mvg,\n" +
				  "A4");
 
			Transport.send(message);
			
//			actual mail to customer
			InternetAddress[] addressTo = new InternetAddress[destAddr.length];
            for (int i = 0; i < destAddr.length; i++) {
                addressTo[i] = new InternetAddress(destAddr[i]);
            }
            message.setRecipients(MimeMessage.RecipientType.TO, addressTo);
            Transport.send(message);

 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}